﻿using Abp.AspNetCore;
using AbpProjectTemplate.Components.Identity;
using AbpProjectTemplate.EntityFrameworkCore;
using AbpProjectTemplate.Utilities.Json;
using AbpProjectTemplate.Web.Authentication;
using AbpProjectTemplate.Web.Cors;
using AbpProjectTemplate.Web.Localization;
using AbpProjectTemplate.Web.Logs;
using AbpProjectTemplate.Web.Swagger;
using AbpProjectTemplate.Web.UEditor;
using Castle.Facilities.Logging;
using Castle.Services.Logging.SerilogIntegration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using Serilog;
using System;

namespace AbpProjectTemplate.Web.Startup
{
    public class Startup
    {
        private readonly IConfiguration _configuration;
        public Startup(IConfiguration configuration)
        {
            this._configuration = configuration;
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {
                //options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
            }).AddNewtonsoftJson();

            services.AddAppIdentity();

            services.AddAppSwagger();

            services.AddAppDbContext(this._configuration);

            services.AddAppAuthentication(this._configuration);

            services.AddUEditorService();

            services.AddAppCors(this._configuration["Cors:Origins"].Split(','));

            //services.AddSignalR();

            //配置json
            services.PostConfigure<MvcNewtonsoftJsonOptions>(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver()
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                };
                options.SerializerSettings.Converters.Add(new DateTimeJsonConverter());
                options.SerializerSettings.Converters.Add(new DecimalJsonConverter());
                options.SerializerSettings.Converters.Add(new EnumJsonConverter());
                options.SerializerSettings.Converters.Add(new TimeSpanToStringConverter());
            });
            // Configure Abp and Dependency Injection
            return services.AddAbp<AppWebModule>(
                // Configure Log4Net logging
                options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                    f => f.LogUsing(new SerilogFactory(Log.Logger)) //f.UseAbpLog4Net().WithConfig("log4net.config")
                )
            );
        }

        public void Configure(IApplicationBuilder app)
        {
            // Initializes ABP framework.
            app.UseAbp(options => { options.UseAbpRequestLocalization = false; });

            app.UseAppCors(); // Enable CORS!

            app.UseDefaultFiles();

            app.UseStaticFiles();

            app.UseRouting();

            //语言获取
            app.UseAppRequestLocalization();

            //认证
            app.UseAuthentication();

            app.UseMiddleware<SecurityStampVerificationMiddleware>();

            //用户安全令牌验证
            app.UseSecurityStampVerification();

            //授权
            app.UseAuthorization();

            //日志目录访问
            app.UseLogBrowser();

            app.UseEndpoints(endpoints =>
            {
                //endpoints.MapHub<AbpCommonHub>("/signalr");
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
            });

            app.UseAppSwagger();
        }
    }
}

